package com.guigu.liuzhireng.interceptor;

import com.guigu.liuzhireng.pojo.Account;
import org.springframework.stereotype.Controller;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Controller
public class LoginInterceptor implements HandlerInterceptor {


    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("拦截");

        Account user = (Account) request.getSession().getAttribute("user");

        String requestURI = request.getRequestURI();

        String [] strs  = new String[]{".js",".css",".png",".jpg",".html",".jpeg",".jfif",".gif"};
        for (String s : strs) {
            if (requestURI.contains(s)) {
                return true;
            }
        }

        if (user!=null || requestURI.trim().contains("/Login.do") || requestURI.trim().contains("/Register.do") ||
                requestURI.trim().contains("/ChangePWD.do") ||   requestURI.trim().contains("/Account.do/Register")){
            return true;
        }else {
            response.sendRedirect("login.html");
            return false;
        }
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

    }
}
